package com.learn.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.learn.core.domain.LOrganization;
import com.learn.core.domain.LQuestionsSort;
import com.learn.core.mapper.LQuestionsSortMapper;
import com.learn.core.service.ILQuestionsSortService;
import com.learn.framework.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Description
 *
 * @author zhaohongyu
 * @ClassName LQuestionsSortServiceImpl
 * @date 2021.03.22 09:45
 */
@Service
public class LQuestionsSortServiceImpl extends BaseServiceImpl<LQuestionsSortMapper, LQuestionsSort> implements ILQuestionsSortService {
    @Autowired
    LQuestionsSortMapper lQuestionsSortMapper;

    @Override
    public List<LQuestionsSort> queryLQuestionsSortAll(QueryWrapper<LQuestionsSort> queryWrapper) {
        List<LQuestionsSort> treeMenus =new ArrayList<>();
        List<LQuestionsSort> lQuestionsSortList = lQuestionsSortMapper.selectList(queryWrapper);
        // 查询树结构
        for(LQuestionsSort lQuestionsSort : getRootNode(lQuestionsSortList)) {
            lQuestionsSort=buildChilTree(lQuestionsSort,lQuestionsSortList);
            treeMenus.add(lQuestionsSort);
        }
        return treeMenus;
    }

    //递归，建立子树形结构
    private LQuestionsSort buildChilTree(LQuestionsSort pNode,List<LQuestionsSort> menuList){
        List<LQuestionsSort> chilMenus =new ArrayList<>();
        for(LQuestionsSort menuNode : menuList) {
            if(menuNode.getParentNode() == pNode.getQuestionsSortId()) {
                chilMenus.add(buildChilTree(menuNode,menuList));
            }
        }
        pNode.setChilLQuestionsSort(chilMenus);
        return pNode;
    }

    //获取根节点
    private List<LQuestionsSort> getRootNode(List<LQuestionsSort> menuList) {
        List<LQuestionsSort> rootMenuLists =new  ArrayList<LQuestionsSort>();
        for(LQuestionsSort menuNode : menuList) {
            if(menuNode.getParentNode() == 0) {
                rootMenuLists.add(menuNode);
            }
        }
        return rootMenuLists;
    }
}
